﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace AllProAutoUpDataClient
{
    internal class Program
    {
        enum TaskTypeEnum
        {
            GetEdition,
            Down
        }
        static TcpClient tcp;
        static FileDB db;
        static void Main(string[] args)
        {
            db = new FileDB();
            tcp = new TcpClient();
            try
            {
                tcp.Connect(db.Model.IP, db.Model.Port);
            }
            catch
            {
                Console.WriteLine("链接失败 ");
            }

            while (true)
            {
                if (tcp.Client != null && tcp.Client.Connected)
                {
                    var data = new
                    {
                        rtype = TaskTypeEnum.GetEdition
                    };
                    var jstr = JsonConvert.SerializeObject(data);
                    var byteDate = Encoding.UTF8.GetBytes(jstr);
                    var dst = tcp.Client.Send(byteDate);
                    Console.WriteLine("发送请求 "+ dst);
                    if (tcp.Available > 0)
                    {
                        var date = new byte[tcp.Available];
                        tcp.Client.Receive(date, tcp.Available, SocketFlags.None);
                        // Encoding.UTF8.GetBytes(date);
                        var obj = Encoding.UTF8.GetString(date);
                        try
                        {
                            var mm = JsonConvert.DeserializeObject<MessageMode>(obj);
                            if (mm.Edition != db.Model.Edition)
                            {
                                Console.WriteLine("版本有更新 开始更新");
                                MyFtp ftp = new MyFtp();
                                ftp.ftpPassword = mm.FtpPass;
                                ftp.ftpUserID = mm.FtpUser;
                                ftp.ftpServerIP = db.Model.IP;
                                var list = ftp.FTPGetFileList();
                                for (int i = 0; i < list.Count; i++)
                                {
                                    Console.WriteLine("开始下载" + list.Count + "-" + i + list[i]);
                                    ftp.FTPDownloadFile(AppDomain.CurrentDomain.BaseDirectory+db.Model.Path,
                                        Path.GetFileName(list[i]), list[i]);
                                    Console.WriteLine("下载完成" + list.Count + "-" + i + list[i]);
                                }
                                db.Model.Edition = mm.Edition;
                                db.Model.EditionTime = DateTime.Now;
                                db.Updata();
                            }
                            else
                            {
                                Console.WriteLine("版本一致 不用更新");
                            }
                        }
                        catch
                        {

                        }
                    }
                }
                else
                {
                    tcp.Close();
                    tcp = null;
                    tcp = new TcpClient();
                    try
                    {
                        tcp.Connect(db.Model.IP, db.Model.Port);
                    }
                    catch
                    {

                    }

                }
                Thread.Sleep(5000);
            }
        }
    }
}
